﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Data.SqlClient;

/// <summary>
/// Summary description for DSSanPham
/// </summary>
public class DSSanPham
{
    SanPhamDA sda = new SanPhamDA();

    public DataTable search(string sp) {
        return getSelectTable( sda.Search(sp),sp); 
    }

    public DataTable getSelectTable(DataTable dt,string ma)
    {
        DataTable getdt = new DataTable();
        try
        {
            IEnumerable<DataRow> query =
            from dtt in dt.AsEnumerable()
            where dtt.Field<String>("MaSanPham").ToLower() == ma.ToLower()
            select dtt;
            getdt = query.CopyToDataTable<DataRow>();
            return getdt;
        }
        catch (Exception)
        {
            return getdt;
            
        }
        
            
        
    }
    public DataTable getAll(object hangid, object loaiid, object chucnangid,object tugia, object dengia)
    {
        DataTable getdt= new DataTable();
        getdt=sda.getAll();
        try
        {
            if(hangid!=null)
            {
                DataTable dt = getdt.Select("NhanHieuID = "+ Convert.ToInt32(hangid)).CopyToDataTable<DataRow>();
                getdt=new DataTable();
                getdt = dt;
            }
            if(loaiid!=null)
            {
                DataTable dt = getdt.Select("LoaiID = " + Convert.ToInt32(loaiid)).CopyToDataTable<DataRow>();
                getdt = new DataTable();
                getdt = dt;
            }
            if(chucnangid!=null)
            {
                DataTable dt = getdt.Select("ChucNangID = " + Convert.ToInt32(chucnangid)).CopyToDataTable<DataRow>();
                getdt = new DataTable();
                getdt = dt;
                
            }
            if (tugia != null)
            {
                DataTable dt = getdt.Select("GiaBan >= " + Convert.ToInt32(tugia)).CopyToDataTable<DataRow>();
                getdt = new DataTable();
                getdt = dt;
                
            }
            if (dengia != null)
            {
                DataTable dt = getdt.Select("GiaBan <= " + Convert.ToInt32(dengia)).CopyToDataTable<DataRow>();
                getdt = new DataTable();
                getdt = dt;
                
            }
            return getdt;
        }
        catch (Exception)
        {
            getdt.Rows.Clear();
            return getdt;
            throw;
        }
        
        
        

        //try
        //{
        //    if(hangid!=null&&loaiid!=null&&chucnangid!=null)
        //    return sda.getDSSanPham(Convert.ToInt32(hangid), Convert.ToInt32(loaiid), Convert.ToInt32(chucnangid));
        //else
        //    if (hangid != null && loaiid != null)
        //    {
        //        return sda.getDSSanPhambyHang_Loai(Convert.ToInt32(hangid), Convert.ToInt32(loaiid));
        //    }
        //    else
        //        if (hangid != null && chucnangid != null)
        //        {
        //            return sda.getDSSanPhambyHang_chucnang(Convert.ToInt32(hangid), Convert.ToInt32(chucnangid));
        //        }else
        //            if (chucnangid != null && loaiid != null)
        //            {
        //                return sda.getDSSanPhambychucnang_Loai(Convert.ToInt32(chucnangid), Convert.ToInt32(loaiid));
        //            }else
        //                if (hangid != null)
        //                {
        //                    return sda.getDSSanPhambyHang(Convert.ToInt32(hangid));
        //                }
        //                else
        //                    if (loaiid != null)
        //                    {
        //                        return sda.getDSSanPhambyLoai(Convert.ToInt32(loaiid));
        //                    }
        //                    else
        //                        if (chucnangid != null)
        //                        {
        //                            return sda.getDSSanPhambyChucNang(Convert.ToInt32(chucnangid));
        //                        }
        //                        else
        //                        {
        //                            return sda.getAll();
        //                        }
        //}
        //catch (Exception)
        //{

        //    throw;
        //}
       

    }

}